package com.joeyzh.widget.scan;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.joey.base.util.DensityUtil;
import com.joeyzh.R;

/**
 * Created by Joey on 2019/1/15.
 *
 * @author by Joey
 *         扫描控件EditText
 */

public class ScanEditText extends AppCompatAutoCompleteTextView {

    private static final int DRAWABLE_LEFT = 0;
    private static final int DRAWABLE_TOP = 1;
    private static final int DRAWABLE_RIGHT = 2;
    private static final int DRAWABLE_BOTTOM = 3;

    private OnScanClickListener onScanClickListener;

    private OnScanResultListener resultListener;

    public ScanEditText(Context context) {
        this(context, null);
    }

    public ScanEditText(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public ScanEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 设置回车监听
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                /**
                 * 监听回车按键，拦截回车按键
                 */
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    selectAll();
                    return onResult(getText(), ScanEditText.this);
                }
                return false;
            }
        });

        // 设置右侧扫描图标
        Drawable rightDraw = context.getResources().getDrawable(R.drawable.ic_scan);
        rightDraw.setBounds(0, 0, rightDraw.getMinimumWidth(), rightDraw.getMinimumHeight());
        setCompoundDrawables(null, null, rightDraw, null);
        setCompoundDrawablePadding(DensityUtil.dip2px(context, getResources().getDimension(R.dimen.layout_min_margin)));

    }

    /**
     * 设置监听扫描图标点检监听
     *
     * @param onScanClickListener
     */
    public void setOnScanClickListener(OnScanClickListener onScanClickListener) {
        this.onScanClickListener = onScanClickListener;
    }

    /**
     * 设置扫描结果监听
     *
     * @param resultListener
     */
    public void setOnScanResultListener(OnScanResultListener resultListener) {
        this.resultListener = resultListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                Drawable drawable = getCompoundDrawables()[DRAWABLE_RIGHT];
                if (drawable != null && event.getX() <= (getWidth() - getPaddingRight())
                        && event.getX() >= (getWidth() - getPaddingRight() - drawable.getBounds().width())) {
                    if (onScanClickListener != null) {
                        onScanClickListener.onClick(this);
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }


    public boolean onResult(CharSequence text, View view) {
        if (resultListener == null) {
            return false;
        } else {
            return resultListener.onResult(text, view);
        }
    }
}
